home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / groff108.lha / groff-1.08 / libgroff / font.cc < prev    next >
C/C++ Source or Header  |  1992-11-24  |  20KB  |  908 lines

  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.      Written by James Clark (jjc@jclark.com)
  4.  
  5. This file is part of groff.
  6.  
  7. groff is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 2, or (at your option) any later
  10. version.
  11.  
  12. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with groff; see the file COPYING.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include <assert.h>
  25. #include <math.h>
  26. #include <stdlib.h>
  27. #include "errarg.h"
  28. #include "error.h"
  29. #include "cset.h"
  30. #include "font.h"
  31. #include "lib.h"
  32.  
  33. const char *const WS = " \t\n\r";
  34.  
  35. struct font_char_metric {
  36.   char type;
  37.   int code;
  38.   int width;
  39.   int height;
  40.   int depth;
  41.   int pre_math_space;
  42.   int italic_correction;
  43.   int subscript_correction;
  44. };
  45.  
  46. struct font_kern_list {
  47.   int i1;
  48.   int i2;
  49.   int amount;
  50.   font_kern_list *next;
  51.  
  52.   font_kern_list(int, int, int, font_kern_list * = 0);
  53. };
  54.  
  55. struct font_widths_cache {
  56.   font_widths_cache *next;
  57.   int point_size;
  58.   int *width;
  59.  
  60.   font_widths_cache(int, int, font_widths_cache *);
  61.   ~font_widths_cache();
  62. };
  63.  
  64. /* text_file */
  65.  
  66. struct text_file {
  67.   FILE *fp;
  68.   char *path;
  69.   int lineno;
  70.   int size;
  71.   int skip_comments;
  72.   char *buf;
  73.   text_file(FILE *fp, char *p);
  74.   ~text_file();
  75.   int next();
  76.   void error(const char *format, 
  77.          const errarg &arg1 = empty_errarg,
  78.          const errarg &arg2 = empty_errarg,
  79.          const errarg &arg3 = empty_errarg);
  80. };
  81.  
  82. text_file::text_file(FILE *p, char *s) 
  83. : lineno(0), buf(0), size(0), skip_comments(1), fp(p), path(s)
  84. {
  85. }
  86.  
  87. text_file::~text_file()
  88. {
  89.   a_delete buf;
  90.   a_delete path;
  91.   if (fp)
  92.     fclose(fp);
  93. }
  94.  
  95.  
  96. int text_file::next()
  97. {
  98.   if (fp == 0)
  99.     return 0;
  100.   if (buf == 0) {
  101.     buf = new char [128];
  102.     size = 128;
  103.   }
  104.   for (;;) {
  105.     int i = 0;
  106.     for (;;) {
  107.       int c = getc(fp);
  108.       if (c == EOF)
  109.     break;
  110.       if (illegal_input_char(c))
  111.     error("illegal input character code `%1'", int(c));
  112.       else {
  113.     if (i + 1 >= size) {
  114.       char *old_buf = buf;
  115.       buf = new char[size*2];
  116.       memcpy(buf, old_buf, size);
  117.       a_delete old_buf;
  118.       size *= 2;
  119.     }
  120.     buf[i++] = c;
  121.     if (c == '\n')
  122.       break;
  123.       }
  124.     }
  125.     if (i == 0)
  126.       break;
  127.     buf[i] = '\0';
  128.     lineno++;
  129.     char *ptr = buf;
  130.     while (csspace(*ptr))
  131.       ptr++;
  132.     if (*ptr != 0 && (!skip_comments || *ptr != '#'))
  133.       return 1;
  134.   }
  135.   return 0;
  136. }
  137.  
  138. void text_file::error(const char *format, 
  139.               const errarg &arg1,
  140.               const errarg &arg2,
  141.               const errarg &arg3)
  142. {
  143.   error_with_file_and_line(path, lineno, format, arg1, arg2, arg3);
  144. }
  145.  
  146.  
  147. /* font functions */
  148.  
  149. font::font(const char *s)
  150. : special(0), ligatures(0), kern_hash_table(0), space_width(0),
  151.   ch(0), ch_used(0), ch_size(0), ch_index(0), nindices(0), widths_cache(0)
  152. {
  153.   name = new char[strlen(s) + 1];
  154.   strcpy(name, s);
  155.   internalname = 0;
  156.   slant = 0.0;
  157.   // load();            // for testing
  158. }
  159.  
  160. font::~font()
  161. {
  162.   a_delete ch;
  163.   a_delete ch_index;
  164.   if (kern_hash_table) {
  165.     for (int i = 0; i < KERN_HASH_TABLE_SIZE; i++) {
  166.       font_kern_list *kerns = kern_hash_table[i];
  167.       while (kerns) {
  168.     font_kern_list *tem = kerns;
  169.     kerns = kerns->next;
  170.     delete tem;
  171.       }
  172.     }
  173.     a_delete kern_hash_table;
  174.   }
  175.   a_delete name;
  176.   a_delete internalname;
  177.   while (widths_cache) {
  178.     font_widths_cache *tem = widths_cache;
  179.     widths_cache = widths_cache->next;
  180.     delete tem;
  181.   }
  182. }
  183.  
  184. static int scale_round(int n, int x, int y)
  185. {
  186.   assert(x >= 0 && y > 0);
  187.   int y2 = y/2;
  188.   if (x == 0)
  189.     return 0;
  190.   if (n >= 0) {
  191.     if (n <= (INT_MAX - y2)/x)
  192.       return (n*x + y2)/y;
  193.     return int(n*double(x)/double(y) + .5);
  194.   }
  195.   else {
  196.     if (-(unsigned)n <= (-(unsigned)INT_MIN - y2)/x)
  197.       return (n*x - y2)/y;
  198.     return int(n*double(x)/double(y) - .5);
  199.   }
  200. }
  201.  
  202. inline int font::scale(int w, int sz)
  203. {
  204.   return sz == unitwidth ? w : scale_round(w, sz, unitwidth);
  205. }
  206.  
  207. int font::get_skew(int c, int point_size, int sl)
  208. {
  209.   int h = get_height(c, point_size);
  210.   return int(h*tan((slant+sl)*M_PI/180.0) + .5);
  211. }
  212.  
  213. int font::contains(int c)
  214. {
  215.   return c >= 0 && c < nindices && ch_index[c] >= 0;
  216. }
  217.  
  218. int font::is_special()
  219. {
  220.   return special;
  221. }
  222.  
  223. font_widths_cache::font_widths_cache(int ps, int ch_size,
  224.                      font_widths_cache *p = 0)
  225. : next(p), point_size(ps)
  226. {
  227.   width = new int[ch_size];
  228.   for (int i = 0; i < ch_size; i++)
  229.     width[i] = -1;
  230. }
  231.  
  232. font_widths_cache::~font_widths_cache()
  233. {
  234.   a_delete width;
  235. }
  236.  
  237. int font::get_width(int c, int point_size)
  238. {
  239.   assert(c >= 0 && c < nindices);
  240.   int i = ch_index[c];
  241.   assert(i >= 0);
  242.  
  243.   if (point_size == unitwidth)
  244.     return ch[i].width;
  245.  
  246.   if (!widths_cache)
  247.     widths_cache = new font_widths_cache(point_size, ch_size);
  248.   else if (widths_cache->point_size != point_size) {
  249.     for (font_widths_cache **p = &widths_cache; *p; p = &(*p)->next)
  250.       if ((*p)->point_size == point_size)
  251.     break;
  252.     if (*p) {
  253.       font_widths_cache *tem = *p;
  254.       *p = (*p)->next;
  255.       tem->next = widths_cache;
  256.       widths_cache = tem;
  257.     }
  258.     else
  259.       widths_cache = new font_widths_cache(point_size, ch_size, widths_cache);
  260.   }
  261.   int &w = widths_cache->width[i];
  262.   if (w < 0)
  263.     w = scale(ch[i].width, point_size);
  264.   return w;
  265. }
  266.  
  267. int font::get_height(int c, int point_size)
  268. {
  269.   assert(c >= 0 && c < nindices && ch_index[c] >= 0);
  270.   return scale(ch[ch_index[c]].height, point_size);
  271. }
  272.  
  273. int font::get_depth(int c, int point_size)
  274. {
  275.   assert(c >= 0 && c < nindices && ch_index[c] >= 0);
  276.   return scale(ch[ch_index[c]].depth, point_size);
  277. }
  278.  
  279. int font::get_italic_correction(int c, int point_size)
  280. {
  281.   assert(c >= 0 && c < nindices && ch_index[c] >= 0);
  282.   return scale(ch[ch_index[c]].italic_correction, point_size);
  283. }
  284.  
  285. int font::get_left_italic_correction(int c, int point_size)
  286. {
  287.   assert(c >= 0 && c < nindices && ch_index[c] >= 0);
  288.   return scale(ch[ch_index[c]].pre_math_space, point_size);
  289. }
  290.  
  291. int font::get_subscript_correction(int c, int point_size)
  292. {
  293.   assert(c >= 0 && c < nindices && ch_index[c] >= 0);
  294.   return scale(ch[ch_index[c]].subscript_correction, point_size);
  295. }
  296.  
  297. int font::get_space_width(int point_size)
  298. {
  299.   return scale(space_width, point_size);
  300. }
  301.  
  302. font_kern_list::font_kern_list(int c1, int c2, int n, font_kern_list *p)
  303.      : i1(c1), i2(c2), amount(n), next(p)
  304. {
  305. }
  306.  
  307. inline int font::hash_kern(int i1, int i2)
  308. {
  309.   int n = ((i1 << 10) + i2) % KERN_HASH_TABLE_SIZE;
  310.   return n < 0 ? -n : n;
  311. }
  312.  
  313. void font::add_kern(int i1, int i2, int amount)
  314. {
  315.   if (!kern_hash_table) {
  316.     kern_hash_table = new font_kern_list *[KERN_HASH_TABLE_SIZE];
  317.     for (int i = 0; i < KERN_HASH_TABLE_SIZE; i++)
  318.       kern_hash_table[i] = 0;
  319.   }
  320.   font_kern_list **p = kern_hash_table + hash_kern(i1, i2);
  321.   *p = new font_kern_list(i1, i2, amount, *p);
  322. }
  323.  
  324. int font::get_kern(int i1, int i2, int point_size)
  325. {
  326.   if (kern_hash_table) {
  327.     for (font_kern_list *p = kern_hash_table[hash_kern(i1, i2)]; p; p = p->next)
  328.       if (i1 == p->i1 && i2 == p->i2)
  329.     return scale(p->amount, point_size);
  330.   }
  331.   return 0;
  332. }
  333.  
  334. int font::has_ligature(int mask)
  335. {
  336.   return mask & ligatures;
  337. }
  338.  
  339. int font::get_character_type(int c)
  340. {
  341.   assert(c >= 0 && c < nindices && ch_index[c] >= 0);
  342.   return ch[ch_index[c]].type;
  343. }
  344.  
  345. int font::get_code(int c)
  346. {
  347.   assert(c >= 0 && c < nindices && ch_index[c] >= 0);
  348.   return ch[ch_index[c]].code;
  349. }
  350.  
  351. const char *font::get_name()
  352. {
  353.   return name;
  354. }
  355.  
  356. const char *font::get_internal_name()
  357. {
  358.   return internalname;
  359. }
  360.  
  361. void font::alloc_ch_index(int index)
  362. {
  363.   if (nindices == 0) {
  364.     nindices = 128;
  365.     if (index >= nindices)
  366.       nindices = index + 10;
  367.     ch_index = new short[nindices];
  368.     for (int i = 0; i < nindices; i++)
  369.       ch_index[i] = -1;
  370.   }
  371.   else {
  372.     int old_nindices = nindices;
  373.     nindices *= 2;
  374.     if (index >= nindices)
  375.       nindices = index + 10;
  376.     short *old_ch_index = ch_index;
  377.     ch_index = new short[nindices];
  378.     memcpy(ch_index, old_ch_index, sizeof(short)*old_nindices);
  379.     for (int i = old_nindices; i < nindices; i++)
  380.       ch_index[i] = -1;
  381.     a_delete old_ch_index;
  382.   }
  383. }
  384.  
  385. void font::extend_ch()
  386. {
  387.   if (ch == 0)
  388.     ch = new font_char_metric[ch_size = 16];
  389.   else {
  390.     int old_ch_size = ch_size;
  391.     ch_size *= 2;
  392.     font_char_metric *old_ch = ch;
  393.     ch = new font_char_metric[ch_size];
  394.     memcpy(ch, old_ch, old_ch_size*sizeof(font_char_metric));
  395.     a_delete old_ch;
  396.   }
  397. }
  398.  
  399. void font::compact()
  400. {
  401.   for (int i = nindices - 1; i >= 0; i--)
  402.     if (ch_index[i] >= 0)
  403.       break;
  404.   i++;
  405.   if (i < nindices) {
  406.     short *old_ch_index = ch_index;
  407.     ch_index = new short[i];
  408.     memcpy(ch_index, old_ch_index, i*sizeof(short));
  409.     a_delete old_ch_index;
  410.     nindices = i;
  411.   }
  412.   if (ch_used < ch_size) {
  413.     font_char_metric *old_ch = ch;
  414.     ch = new font_char_metric[ch_used];
  415.     memcpy(ch, old_ch, ch_used*sizeof(font_char_metric));
  416.     a_delete old_ch;
  417.     ch_size = ch_used;
  418.   }
  419. }
  420.  
  421. void font::add_entry(int index, const font_char_metric &metric)
  422. {
  423.   assert(index >= 0);
  424.   if (index >= nindices)
  425.     alloc_ch_index(index);
  426.   assert(index < nindices);
  427.   if (ch_used + 1 >= ch_size)
  428.     extend_ch();
  429.   assert(ch_used + 1 < ch_size);
  430.   ch_index[index] = ch_used;
  431.   ch[ch_used++] = metric;
  432. }
  433.  
  434. void font::copy_entry(int new_index, int old_index)
  435. {
  436.   assert(new_index >= 0 && old_index >= 0 && old_index < nindices);
  437.   if (new_index >= nindices)
  438.     alloc_ch_index(new_index);
  439.   ch_index[new_index] = ch_index[old_index];
  440. }
  441.  
  442. font *font::load_font(const char *s, int *not_found)
  443. {
  444.   font *f = new font(s);
  445.   if (!f->load(not_found)) {
  446.     delete f;
  447.     return 0;
  448.   }
  449.   return f;
  450. }
  451.  
  452. static char *trim_arg(char *p)
  453. {
  454.   if (!p)
  455.     return 0;
  456.   while (csspace(*p))
  457.     p++;
  458.   char *q = strchr(p, '\0');
  459.   while (q > p && csspace(q[-1]))
  460.     q--;
  461.   *q = '\0';
  462.   return p;
  463. }
  464.  
  465. // If the font can't be found, then if not_found is NULL it will be set
  466. // to 1 otherwise a message will be printed.
  467.  
  468. int font::load(int *not_found)
  469. {
  470.   char *path;
  471.   FILE *fp;
  472.   if ((fp = open_file(name, &path)) == NULL) {
  473.     if (not_found)
  474.       *not_found = 1;
  475.     else
  476.       error("can't find font file `%1'", name);
  477.     return 0;
  478.   }
  479.   text_file t(fp, path);
  480.   t.skip_comments = 1;
  481.   char *p;
  482.   for (;;) {
  483.     if (!t.next()) {
  484.       t.error("missing charset command");
  485.       return 0;
  486.     }
  487.     p = strtok(t.buf, WS);
  488.     if (strcmp(p, "name") == 0) {
  489.     }
  490.     else if (strcmp(p, "spacewidth") == 0) {
  491.       p = strtok(0, WS);
  492.       int n;
  493.       if (p == 0 || sscanf(p, "%d", &n) != 1 || n <= 0) {
  494.     t.error("bad argument for spacewidth command");
  495.     return 0;
  496.       }
  497.       space_width = n;
  498.     }
  499.     else if (strcmp(p, "slant") == 0) {
  500.       p = strtok(0, WS);
  501.       double n;
  502.       if (p == 0 || sscanf(p, "%lf", &n) != 1 || n >= 90.0 || n <= -90.0) {
  503.     t.error("bad argument for slant command", p);
  504.     return 0;
  505.       }
  506.       slant = n;
  507.     }
  508.     else if (strcmp(p, "ligatures") == 0) {
  509.       for (;;) {
  510.     p = strtok(0, WS);
  511.     if (p == 0 || strcmp(p, "0") == 0)
  512.       break;
  513.     if (strcmp(p, "ff") == 0)
  514.       ligatures |= LIG_ff;
  515.     else if (strcmp(p, "fi") == 0)
  516.       ligatures |= LIG_fi;
  517.     else if (strcmp(p, "fl") == 0)
  518.       ligatures |= LIG_fl;
  519.     else if (strcmp(p, "ffi") == 0)
  520.       ligatures |= LIG_ffi;
  521.     else if (strcmp(p, "ffl") == 0)
  522.       ligatures |= LIG_ffl;
  523.     else {
  524.       t.error("unrecognised ligature `%1'", p);
  525.       return 0;
  526.     }
  527.       }
  528.     }
  529.     else if (strcmp(p, "internalname") == 0) {
  530.       p = strtok(0, WS);
  531.       if (!p) {
  532.     t.error("`internalname command requires argument");
  533.     return 0;
  534.       }
  535.       internalname = new char[strlen(p) + 1];
  536.       strcpy(internalname, p);
  537.     }
  538.     else if (strcmp(p, "special") == 0) {
  539.       special = 1;
  540.     }
  541.     else if (strcmp(p, "kernpairs") != 0 && strcmp(p, "charset") != 0) {
  542.       char *command = p;
  543.       p = strtok(0, "\n");
  544.       handle_unknown_font_command(command, trim_arg(p), t.path, t.lineno);
  545.     }
  546.     else
  547.       break;
  548.   }
  549.   char *command = p;
  550.   int had_charset = 0;
  551.   t.skip_comments = 0;
  552.   while (command) {
  553.     if (strcmp(command, "kernpairs") == 0) {
  554.       for (;;) {
  555.     if (!t.next()) {
  556.       command = 0;
  557.       break;
  558.     }
  559.     char *c1 = strtok(t.buf, WS);
  560.     if (c1 == 0)
  561.       continue;
  562.     char *c2 = strtok(0, WS);
  563.     if (c2 == 0) {
  564.       command = c1;
  565.       break;
  566.     }
  567.     p = strtok(0, WS);
  568.     if (p == 0) {
  569.       t.error("missing kern amount");
  570.       return 0;
  571.     }
  572.     int n;
  573.     if (sscanf(p, "%d", &n) != 1) {
  574.       t.error("bad kern amount `%1'", p);
  575.       return 0;
  576.     }
  577.     int i1 = name_to_index(c1);
  578.     if (i1 < 0) {
  579.       t.error("illegal character `%1'", c1);
  580.       return 0;
  581.     }
  582.     int i2 = name_to_index(c2);
  583.     if (i2 < 0) {
  584.       t.error("illegal character `%1'", c2);
  585.       return 0;
  586.     }
  587.     add_kern(i1, i2, n);
  588.       }
  589.     }
  590.     else if (strcmp(command, "charset") == 0) {
  591.       had_charset = 1;
  592.       int last_index = -1;
  593.       for (;;) {
  594.     if (!t.next()) {
  595.       command = 0;
  596.       break;
  597.     }
  598.     char *nm = strtok(t.buf, WS);
  599.     if (nm == 0)
  600.       continue;            // I dont think this should happen
  601.     p = strtok(0, WS);
  602.     if (p == 0) {
  603.       command = nm;
  604.       break;
  605.     }
  606.     if (p[0] == '"') {
  607.       if (last_index == -1) {
  608.         t.error("first charset entry is duplicate");
  609.         return 0;
  610.       }
  611.       if (strcmp(nm, "---") == 0) {
  612.         t.error("unnamed character cannot be duplicate");
  613.         return 0;
  614.       }
  615.       int index = name_to_index(nm);
  616.       if (index < 0) {
  617.         t.error("illegal character `%1'", nm);
  618.         return 0;
  619.       }
  620.       copy_entry(index, last_index);
  621.     }
  622.     else {
  623.       font_char_metric metric;
  624.       metric.height = 0;
  625.       metric.depth = 0;
  626.       metric.pre_math_space = 0;
  627.       metric.italic_correction = 0;
  628.       metric.subscript_correction = 0;
  629.       int nparms = sscanf(p, "%d,%d,%d,%d,%d,%d",
  630.                   &metric.width, &metric.height, &metric.depth,
  631.                   &metric.italic_correction,
  632.                   &metric.pre_math_space,
  633.                   &metric.subscript_correction);
  634.       if (nparms < 1) {
  635.         t.error("bad width for `%1'", nm);
  636.         return 0;
  637.       }
  638.       p = strtok(0, WS);
  639.       if (p == 0) {
  640.         t.error("missing character type for `%1'", nm);
  641.         return 0;
  642.       }
  643.       int type;
  644.       if (sscanf(p, "%d", &type) != 1) {
  645.         t.error("bad character type for `%1'", nm);
  646.         return 0;
  647.       }
  648.       if (type < 0 || type > 255) {
  649.         t.error("character code `%1' out of range", type);
  650.         return 0;
  651.       }
  652.       metric.type = type;
  653.       p = strtok(0, WS);
  654.       if (p == 0) {
  655.         t.error("missing code for `%1'", nm);
  656.         return 0;
  657.       }
  658.       char *ptr;
  659.       metric.code = (int)strtol(p, &ptr, 0);
  660.       if (metric.code == 0 && ptr == p) {
  661.         t.error("bad code `%1' for character `%2'", p, nm);
  662.         return 0;
  663.       }
  664.       if (strcmp(nm, "---") == 0) {
  665.         last_index = number_to_index(metric.code);
  666.         add_entry(last_index, metric);
  667.       }
  668.       else {
  669.         last_index = name_to_index(nm);
  670.         if (last_index < 0) {
  671.           t.error("illegal character `%1'", nm);
  672.           return 0;
  673.         }
  674.         add_entry(last_index, metric);
  675.         copy_entry(number_to_index(metric.code), last_index);
  676.       }
  677.     }
  678.       }
  679.       if (last_index == -1) {
  680.     t.error("I didn't seem to find any characters");
  681.     return 0;
  682.       }
  683.     }
  684.     else {
  685.       t.error("unrecognised command `%1' after `kernpairs' or `charset' command", command);
  686.       return 0;
  687.     }
  688.   }
  689.   if (!had_charset) {
  690.     t.error("missing charset command");
  691.     return 0;
  692.   }
  693.   if (space_width == 0)
  694.     space_width = scale_round(unitwidth, res, 72*3*sizescale);
  695.   compact();
  696.   return 1;
  697. }
  698.  
  699. static struct {
  700.   const char *command;
  701.   int *ptr;
  702. } table[] = {
  703.   "res", &font::res,
  704.   "hor", &font::hor,
  705.   "vert", &font::vert,
  706.   "unitwidth", &font::unitwidth,
  707.   "paperwidth", &font::paperwidth,
  708.   "paperlength", &font::paperlength,
  709.   "spare1", &font::biggestfont,
  710.   "biggestfont", &font::biggestfont,
  711.   "spare2", &font::spare2,
  712.   "sizescale", &font::sizescale
  713.   };
  714.  
  715.  
  716. int font::load_desc()
  717. {
  718.   int nfonts = 0;
  719.   FILE *fp;
  720.   char *path;
  721.   if ((fp = open_file("DESC", &path)) == 0) {
  722.     error("can't find `DESC' file");
  723.     return 0;
  724.   }
  725.   text_file t(fp, path);
  726.   t.skip_comments = 1;
  727.   res = 0;
  728.   while (t.next()) {
  729.     char *p = strtok(t.buf, WS);
  730.     int found = 0;
  731.     for (int i = 0; !found && i < sizeof(table)/sizeof(table[0]); i++)
  732.       if (strcmp(table[i].command, p) == 0)
  733.     found = 1;
  734.     if (found) {
  735.       char *q = strtok(0, WS);
  736.       if (!q) {
  737.     t.error("missing value for command `%1'", p);
  738.     return 0;
  739.       }
  740.       //int *ptr = &(this->*(table[i-1].ptr));
  741.       int *ptr = table[i-1].ptr;
  742.       if (sscanf(q, "%d", ptr) != 1) {
  743.     t.error("bad number `%1'", q);
  744.     return 0;
  745.       }
  746.     }
  747.     else if (strcmp("tcommand", p) == 0) {
  748.       tcommand = 1;
  749.     }
  750.     else if (strcmp("family", p) == 0) {
  751.       p = strtok(0, WS);
  752.       if (!p) {
  753.     t.error("family command requires an argument");
  754.     return 0;
  755.       }
  756.       char *tem = new char[strlen(p)+1];
  757.       strcpy(tem, p);
  758.       family = tem;
  759.     }
  760.     else if (strcmp("fonts", p) == 0) {
  761.       p = strtok(0, WS);
  762.       if (!p || sscanf(p, "%d", &nfonts) != 1 || nfonts <= 0) {
  763.     t.error("bad number of fonts `%1'", p);
  764.     return 0;
  765.       }
  766.       font_name_table = (const char **)new char *[nfonts+1]; 
  767.       for (int i = 0; i < nfonts; i++) {
  768.     p = strtok(0, WS);
  769.     while (p == 0) {
  770.       if (!t.next()) {
  771.         t.error("end of file while reading list of fonts");
  772.         return 0;
  773.       }
  774.       p = strtok(t.buf, WS);
  775.     }
  776.     char *temp = new char[strlen(p)+1];
  777.     strcpy(temp, p);
  778.     font_name_table[i] = temp;
  779.       }
  780.       p = strtok(0, WS);
  781.       if (p != 0) {
  782.     t.error("font count does not match number of fonts");
  783.     return 0;
  784.       }
  785.       font_name_table[nfonts] = 0;
  786.     }
  787.     else if (strcmp("sizes", p) == 0) {
  788.       int n = 16;
  789.       sizes = new int[n];
  790.       int i = 0;
  791.       for (;;) {
  792.     p = strtok(0, WS);
  793.     while (p == 0) {
  794.       if (!t.next()) {
  795.         t.error("list of sizes must be terminated by `0'");
  796.         return 0;
  797.       }
  798.       p = strtok(t.buf, WS);
  799.     }
  800.     int lower, upper;
  801.     switch (sscanf(p, "%d-%d", &lower, &upper)) {
  802.     case 1:
  803.       upper = lower;
  804.       // fall through
  805.     case 2:
  806.       if (lower <= upper && lower >= 0)
  807.         break;
  808.       // fall through
  809.     default:
  810.       t.error("bad size range `%1'", p);
  811.       return 0;
  812.     }
  813.     if (i + 2 > n) {
  814.       int *old_sizes = sizes;
  815.       sizes = new int[n*2];
  816.       memcpy(sizes, old_sizes, n*sizeof(int));
  817.       n *= 2;
  818.       a_delete old_sizes;
  819.     }
  820.     sizes[i++] = lower;
  821.     if (lower == 0)
  822.       break;
  823.     sizes[i++] = upper;
  824.       }
  825.       if (i == 1) {
  826.     t.error("must have some sizes");
  827.     return 0;
  828.       }
  829.     }
  830.     else if (strcmp("styles", p) == 0) {
  831.       int style_table_size = 5;
  832.       style_table = (const char **)new char *[style_table_size];
  833.       for (int j = 0; j < style_table_size; j++)
  834.     style_table[j] = 0;
  835.       int i = 0;
  836.       for (;;) {
  837.     p = strtok(0, WS);
  838.     if (p == 0)
  839.       break;
  840.     // leave room for terminating 0
  841.     if (i + 1 >= style_table_size) {
  842.       const char **old_style_table = style_table;
  843.       style_table_size *= 2;
  844.       style_table = (const char **)new char*[style_table_size];
  845.       for (j = 0; j < i; j++)
  846.         style_table[j] = old_style_table[j];
  847.       for (; j < style_table_size; j++)
  848.         style_table[j] = 0;
  849.       a_delete old_style_table;
  850.     }
  851.     char *tem = new char[strlen(p) + 1];
  852.     strcpy(tem, p);
  853.     style_table[i++] = tem;
  854.       }
  855.     }
  856.     else if (strcmp("charset", p) == 0)
  857.       break;
  858.     else if (unknown_desc_command_handler) {
  859.       char *command = p;
  860.       p = strtok(0, "\n");
  861.       (*unknown_desc_command_handler)(command, trim_arg(p), t.path, t.lineno);
  862.     }
  863.   }
  864.   if (res == 0) {
  865.     t.error("missing `res' command");
  866.     return 0;
  867.   }
  868.   if (unitwidth == 0) {
  869.     t.error("missing `unitwidth' command");
  870.     return 0;
  871.   }
  872.   if (font_name_table == 0) {
  873.     t.error("missing `fonts' commmand");
  874.     return 0;
  875.   }
  876.   if (sizes == 0) {
  877.     t.error("missing `sizes' command");
  878.     return 0;
  879.   }
  880.   if (sizescale < 1) {
  881.     t.error("bad `sizescale' value");
  882.     return 0;
  883.   }
  884.   if (hor < 1) {
  885.     t.error("bad `hor' value");
  886.     return 0;
  887.   }
  888.   if (vert < 1) {
  889.     t.error("bad `vert' value");
  890.     return 0;
  891.   }
  892.   return 1;
  893. }      
  894.  
  895. void font::handle_unknown_font_command(const char *, const char *,
  896.                        const char *, int)
  897. {
  898. }
  899.  
  900. FONT_COMMAND_HANDLER
  901. font::set_unknown_desc_command_handler(FONT_COMMAND_HANDLER func)
  902. {
  903.   FONT_COMMAND_HANDLER prev = unknown_desc_command_handler;
  904.   unknown_desc_command_handler = func;
  905.   return prev;
  906. }
  907.  
  908.